using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Stopwatch
{
    public partial class ViewLapSummary : Form
    {
        private RunReport runReport;
        private string XMLFilePath;

        private ViewLapSummary()
        { 
        
        }

        public ViewLapSummary(string XMLPath)
        {
            XMLFilePath = XMLPath; 
            InitializeComponent();
        }

        private void lsViewReport_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void ViewLapSummary_Load(object sender, EventArgs e)
        {

            runReport = new RunReport();

            LoadListView();

        }

        private void LoadListView()
        {

            DataSet dsLapSummary = new DataSet("RunReport");
            DataTable dtLaps = new DataTable("LapsTable");
            DataTable dtSummary = new DataTable("SummaryTable");

            try
            {
                string sLapInfo = string.Empty;
                string sFastest = string.Empty;
                string sSlowest = string.Empty;
                string sAverage = string.Empty;

                dsLapSummary = runReport.GetLapSummaryDetails(XMLFilePath);
                dtLaps = dsLapSummary.Tables["LapsTable"];
                dtSummary = dsLapSummary.Tables["SummaryTable"];

                for (int i = dtLaps.Rows.Count - 1; i >= 0 ; i--)
                {
                    sLapInfo = dtLaps.Rows[i]["LapInfo"].ToString();
                    lsLapSummary.Items.Add(new ListViewItem(new string[] { sLapInfo  }));
                }

                sFastest = String.Format("Fastest:\r\n{0}", dtSummary.Rows[0]["Fastest"].ToString());
                sSlowest = String.Format("Slowest:\r\n{0}", dtSummary.Rows[0]["Slowest"].ToString());
                sAverage = String.Format("Average:\r\n{0}", dtSummary.Rows[0]["Average"].ToString());

                lbFastest.Text = sFastest;
                lbSlowest.Text = sSlowest;
                lbAverage.Text = sAverage;

                if (lsLapSummary.Items.Count > 0)
                {
                    lsLapSummary.Items[0].Selected = true;
                    lsLapSummary.Items[0].Focused  = true;
                }

                lsLapSummary.Refresh();

            }
            catch (Exception ex)
            {
                Debug.WriteLine(String.Format("Stopwatch LoadListView Exception Msg: {0} Stack Info: {1}",
                    ex.Message, ex.StackTrace));
            }
            finally
            {
                dtLaps.Dispose();
                dtSummary.Dispose();
                dsLapSummary.Dispose();
            }

        }

        private void CloseViewLapSummary()
        {
            //Close form and free probable resources
            this.Close();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            CloseViewLapSummary(); 
        }


    }
}